// 表格功能
import { ref,toRaw } from 'vue'
import { ElMessage } from 'element-plus'
import { getTableData } from '@/utils/api'
import { downloadData } from "@/utils/func";
import { baseurl,amountList } from '@/utils/constant'
import { type FormSearchInterface } from "@/utils/interface"
import { useTableStore } from '@/stores/useTableStore';

export default function(){
    const tableStore = useTableStore();
    tableStore.detailType = 1
    
    // 数据整合
    function dataFormat(params:FormSearchInterface){
      // 收支类型
      let obj :any = {...toRaw(params)}
          obj.pageNum      = tableStore.pagination.currentPage
          obj.pageSize     = tableStore.pagination.pageSize
      if(obj.operationType === 0){
        obj.operationType = undefined
      }
      // 金额范围
      if(obj.amount && obj.amount != 5){
        let arr = amountList.filter(item=>item.id == obj.amount)[0]['value']
        obj.smallAmount = arr[0]
        obj.bigAmount   = arr[1]
      }else if(obj.amount === 5 && params.customMoneyArea && params.customMoneyArea !== ""){
        console.log("自定义金额:",params.customMoneyArea);
        let arr = (params.customMoneyArea as string).split('-')
        obj.smallAmount = arr[0]
        obj.bigAmount   = Number(arr[1]) > 5000000 ? 5000000 : arr[1]
      }else{
        obj.smallAmount = undefined
        obj.bigAmount   = undefined
      }
      // 时间范围
      if(params['date']){
        let date1 = params['date'][0]
        let date2 = params['date'][1]
        obj.startTime = `${date1.getFullYear()}-${date1.getMonth()+1}-${date1.getDate()}`;
        obj.endTime = `${date2.getFullYear()}-${date2.getMonth()+1}-${date2.getDate()}`;
      }
      delete obj.date
      console.log("搜索数据条件:",obj);
      return obj
    }
    function getTableList(){  
      tableStore.tableLoading = true
      let obj = dataFormat(tableStore.form)
        getTableData(obj).then(res=>{
          if(res.data.code == 200){
              tableStore.pagination.total= res.data.data[0]['total']
              tableStore.tableList       = res.data.data[0]['list'];
              tableStore.tableLoading    = false
              tableStore.tableUpdateTime = res.data.data[0]['currentTime']
              console.log('更新表格数据:',res.data);
          }else{
              ElMessage({
                  message : res.data.msg,
                  type    : 'error',
                  duration: 2 * 1000
              })
              tableStore.tableLoading = false
          }
        }).catch(err=>{
            ElMessage({
                message : err,
                type    : 'error',   
                duration: 2 * 1000         
            })
            tableStore.tableLoading = false
        })
    }
  
    // 下载全部数据
    function downloadAllData(){
        // 搜索条件发送到接口
        let obj = dataFormat(tableStore.form)
        console.log('下载全部数据:',obj);
        let startTime     = obj.startTime;
        let endTime       = obj.endTime;
        let bigAmount     = obj.bigAmount;
        let smallAmount   = obj.smallAmount;
        let operationType = obj.operationType;
        let searchKey     = obj.searchKey;
        let url           = `${baseurl}/withdraw/getDownLoad?type=0&`;
        // 拼接参数
        if(startTime){
          url += `startTime=${startTime}&`
        }
        if(endTime){
          url += `endTime=${endTime}&`
        }
        if(operationType || operationType === 0){
          url += `operationType=${operationType}&`
        }
        if(searchKey){
          url += `searchKey=${searchKey}&`
        }
        if(smallAmount || smallAmount === 0){
          url += `smallAmount=${smallAmount}&`
        }
        if(bigAmount || bigAmount === 0){
          url += `bigAmount=${bigAmount}&`
        }
        if(url.endsWith('&')){
          url = url.slice(0,-1)
        }
        let a = document.createElement('a');
            a.href   = url;
            a.target = "_blank";
            a.click();
            a.remove()
    }

  return {getTableList,downloadData,downloadAllData}
}






